import 'package:logger/logger.dart';
import 'package:sqflite/sqflite.dart';

import '../pojo/Enums.dart';
import '../pojo/ImPojo.dart';
import '../util/DateUtil.dart';
import '../util/ImSingleton.dart';
import '../util/MapUtil.dart';
import 'BaseDao.dart';

class ImLoginDao extends BaseDao {

  final _logger = Logger(
    printer: PrettyPrinter(),
  );

  Future<ImLogin?> getLoginByUserId(String userId) async {
    Database? database = getDatabase(); //ImSingleton.getInstance()?.dataBase;
    List? list = await database?.rawQuery("select * from im_cache_login_user where userId = ?", [userId]);
    if (list != null && list.length > 0) {
      return Future.value(_toImLogin(list[0]));
    }
    return Future.value(null);
  }

  Future<ImLogin?> getLoginForLasted() async {
    Database? database = getDatabase(); //ImSingleton.getInstance()?.dataBase;
    List? list = await database?.rawQuery("select * from im_cache_login_user where token != '' order by updateTime desc");
    if (list != null && list.length > 0) {
      return Future.value(_toImLogin(list[0]));
    }
    return Future.value(null);
  }

  Future<int> insert(ImLogin login) async {
    Database? database = getDatabase(); //ImSingleton.getInstance()?.dataBase;
    var data = _toInsertValueMap(login);
    int? id = await database?.insert("im_cache_login_user", data);
    assert(id != null && id > 0, "insert im_cache_login_user error");
    login.id = id;
    _logger.i("dao insert im_cache_login_user: $id");
    return Future.value(id);
  }

  Future<List<ImLogin>> getLogins() async {
    Database? database = getDatabase(); //ImSingleton.getInstance()?.dataBase;
    String sql = "select * from im_cache_login_user";
    List<Map<String, Object?>>? list = await database?.rawQuery(sql, []);
    return Future.value(list?.map((map)=>_toImLogin(map)).toList());
  }

  Future<int?> update(ImLogin login) async {
    Database? database = getDatabase(); //ImSingleton.getInstance()?.dataBase;
    int? total = await database?.update("im_cache_login_user", _toUpdateValueMap(login), where: "userId = ?",
        whereArgs: [login.userId]);
    return total;
  }

  ImLogin _toImLogin(Map<String, dynamic> map) {
    LoginType loginType = LoginType.ofValue(MapUtil.getInt(map, "loginType"));
    var login = ImLogin(userId: MapUtil.getString(map, "userId"), loginType: loginType);
    login.id = MapUtil.getInt(map, "id");
    login.token = MapUtil.getStringOrNull(map, "token");
    login.maskPassword = MapUtil.getStringOrNull(map, "maskPassword");
    login.updateTime =  MapUtil.getDate(map, 'updateTime');
    return login;
  }

  Map<String, Object?> _toInsertValueMap(ImLogin login) {
    Map<String, Object?> map = {};
    map['userId'] = login.userId;
    map["token"]= login.token;
    map['loginType'] = login.loginType.value;
    map['maskPassword'] = login.maskPassword;
    map['updateTime'] = DateUtil.formatDate(login.updateTime);
    return map;
  }

  Map<String, Object?> _toUpdateValueMap(ImLogin login) {
    Map<String, Object?> map = {};
    map["token"]= login.token;
    map['maskPassword'] = login.maskPassword;
    map['updateTime'] = DateUtil.formatDate(login.updateTime);
    map['loginType'] = login.loginType.value;
    return map;
  }
}